package com.wanli.storemobile.mine;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseActivity;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.bean.StoreProfileBean;
import com.wanli.storemobile.bean.UserInfoBean;
import com.wanli.storemobile.bean.WithdrawListBean;
import com.wanli.storemobile.homepage.WithdrawListActivity;
import com.wanli.storemobile.mine.adapter.WithdrawListAdapter;
import com.wanli.storemobile.mine.model.IMineModel;
import com.wanli.storemobile.mine.model.MineModelImpl;
import com.wanli.storemobile.utils.FastClickUtils;
import com.wanli.storemobile.utils.NumberFormatUtils;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.AppTitleBar;
import com.wanli.storemobile.widget.ViewLoading;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private IMineModel mineModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mineModel = new MineModelImpl();

        titleBar.setRightClickListener(() -> {
            showActivity(WithdrawListActivity.class);
        });
        requestUserInfo();
        requestStoreProfile();
    }

    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getCan_withdrawal_money()) / 100);
                tvMoney.setText(money);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void requestStoreProfile() {
        mineModel.requestStoreProfile(new DataCallBack<StoreProfileBean>() {
            @Override
            public void onSuccessful(StoreProfileBean bean) {
                tvBankCard.setText("到账银行卡：" + bean.getData().getBank_card_no());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void requestWithdrawalAdd(String money) {
        ViewLoading.showProgress(mActivity, "提交中......");
        mineModel.requestWithdrawalAdd(money, "", new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
                showActivity(WithdrawListActivity.class);
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        String money = editMoney.getText().toString();
        if (TextUtils.isEmpty(money)) {
            ToastUtil.showShort("请输入提现金额");
            return;
        }
        if (FastClickUtils.isFastClick()) {
            return;
        }
        //钱数 单位分
        String moneyUnit = String.valueOf(Double.parseDouble(money) * 100);
        moneyUnit = moneyUnit.substring(0, moneyUnit.indexOf("."));
        requestWithdrawalAdd(moneyUnit);
    }
}
